package net.lab1024.sa.admin.module.business.pdf.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.config.PdfConfig;
import net.lab1024.sa.admin.module.business.pdf.service.PdfParseService;
import net.lab1024.sa.admin.module.business.pdf.util.OcrConfigChecker;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * PDF解析控制器
 *
 * @Author: SmartAdmin
 * @Date: 2024-01-01
 * @Copyright: 1024创新实验室
 */
@Tag(name = "PDF解析管理")
@RestController
@RequestMapping("/pdf")
@Slf4j
public class PdfController {

    @Resource
    private PdfParseService pdfParseService;
    
    @Resource
    private PdfConfig pdfConfig;
    
    @Resource
    private OcrConfigChecker ocrConfigChecker;

    /**
     * 上传并解析PDF文件
     */
    @PostMapping("/upload")
    @Operation(summary = "上传并解析PDF文件")
    public ResponseDTO<Long> uploadAndParse(@RequestParam("file") MultipartFile file,
                                           @RequestParam("uploadUserId") Long uploadUserId) {
        try {
            // 检查文件类型
            if (!file.getOriginalFilename().toLowerCase().endsWith(".pdf")) {
                return ResponseDTO.userErrorParam("只支持PDF文件上传");
            }
            
            // 检查文件大小
            long maxSize = pdfConfig.getUploadMaxFileSize() * 1024 * 1024L;
            if (file.getSize() > maxSize) {
                return ResponseDTO.userErrorParam("文件大小不能超过 " + pdfConfig.getUploadMaxFileSize() + "MB");
            }
            
            Long documentId = pdfParseService.parseAndSave(file, uploadUserId);
            return ResponseDTO.ok(documentId);
        } catch (Exception e) {
            log.error("PDF文件上传解析失败", e);
            return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "PDF文件上传解析失败: " + e.getMessage());
        }
    }

    /**
     * 检查OCR配置状态
     */
    @GetMapping("/ocr/check")
    @Operation(summary = "检查OCR配置状态")
    public ResponseDTO<OcrConfigChecker.OcrConfigStatus> checkOcrConfig() {
        try {
            OcrConfigChecker.OcrConfigStatus status = ocrConfigChecker.checkOcrConfig(pdfConfig.getTessdataPath());
            return ResponseDTO.ok(status);
        } catch (Exception e) {
            log.error("检查OCR配置失败", e);
            return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "检查OCR配置失败: " + e.getMessage());
        }
    }

    /**
     * 获取PDF配置信息
     */
    @GetMapping("/config")
    @Operation(summary = "获取PDF配置信息")
    public ResponseDTO<PdfConfig> getPdfConfig() {
        try {
            return ResponseDTO.ok(pdfConfig);
        } catch (Exception e) {
            log.error("获取PDF配置失败", e);
            return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "获取PDF配置失败: " + e.getMessage());
        }
    }
} 